<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/2
 * Time: 17:09
 */

namespace app\admin\controller;


use app\admin\service\RuleService;
use app\Request;
use think\App;

class Rule extends AdminBase
{
    protected $ruleService = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->ruleService = new RuleService();
    }

    public function index()
    {
        $all_top_permissions = $this->getAllTopPermissions();

        return view('rule/index', compact('all_top_permissions'));
    }

    public function create()
    {
        $all_top_permissions = $this->getAllTopPermissions();

        return view('rule/create', compact('all_top_permissions'));
    }

    private function _postData(Request $request)
    {
        return [
            'pid'       => $request->post('pid', 0, 'intval'),
            'name'      => $request->post('name', '', 'trim'),
            'title'     => $request->post('title', '', 'trim'),
            'type'      => $request->post('type', 1, 'intval'),
            'condition' => $request->post('condition', '', 'trim'),
            'icon'      => $request->post('icon', '', 'trim'),
            'is_menu'   => $request->post('is_menu', 0, 'intval'),
            'status'    => $request->post('status', 0, 'intval'),
        ];
    }

    public function save(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "请求类型不合法");
        }

        $data = $this->_postData($request);

        $res = $this->validate($data, "RuleValidate.add");
        if (true !== $res) {
            return writeJson(config('status.error'), $res);
        }

        try {
            $result = $this->ruleService->addRule($data);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($result) {
            return writeJson(config('status.success'), "添加成功");
        }

        return writeJson(config('status.error'), "添加失败");
    }

    public function edit($id)
    {
        try {
            $rule_one = $this->ruleService->getOneRuleById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $all_top_permissions = $this->getAllTopPermissions();

        if ($rule_one) {
            return view('rule/edit', compact('rule_one', 'all_top_permissions'));
        }

        $this->error('该权限规则不存在');
    }

    public function update(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "请求类型不合法");
        }

        $data = [
            'id'        => $request->post('id', 0, 'intval'),
            'pid'       => $request->post('pid', 0, 'intval'),
            'name'      => $request->post('name', '', 'trim'),
            'title'     => $request->post('title', '', 'trim'),
            'type'      => $request->post('type', 1, 'intval'),
            'condition' => $request->post('condition', '', 'trim'),
            'icon'      => $request->post('icon', '', 'trim'),
            'is_menu'   => $request->post('is_menu', 0, 'intval'),
            'status'    => $request->post('status', 0, 'intval'),
        ];

        $res = $this->validate($data, "RuleValidate.edit");
        if (true !== $res) {
            return writeJson(config('status.error'), $res);
        }

        $updateData = $this->_postData($request);

        try {
            $result = $this->ruleService->updateRuleById($data['id'], $updateData);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($result) {
            return writeJson(config('status.success'), "编辑成功");
        }

        return writeJson(config('status.error'), "编辑失败");
    }

    public function delete(Request $request)
    {
        if (!$request->isPost() || !$request->isAjax()) {
            return writeJson(config('status.error'), "非法请求类型");
        }

        $id = $request->post('id', 0, 'intval');
        try {
            $res = $this->ruleService->deleteById($id);
        } catch (\Exception $e) {
            return writeJson(config('status.error'), $e->getMessage());
        }

        if ($res) {
            return writeJson(config('status.success'), '删除成功');
        }

        return writeJson(config('status.error'), '删除失败');
    }
}